﻿@model IEnumerable<ExpressQuiz.ViewModels.QuizViewModel> 


    @Scripts.Render("~/bundles/expressquiz")


@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "__AjaxAntiForgeryForm" }))
{
    @Html.AntiForgeryToken()
}

<h3>My quizzes</h3>





@{
    var quizzes = Model.ToList();
    if (quizzes.Any())
    {




        <table class="table table-striped table-hover ">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Questions</th>
                    <th>Locked</th>
                    <th>Delete</th>
                </tr>
            </thead>

            <tbody>

                @{

        foreach (var q in quizzes)
                    {

                        <tr @(q.Locked ? "class=danger" : "")>
                            <td>@Html.ActionLink(@q.Name, "Details", "Quizzes", new {id = q.QuizId}, htmlAttributes: new {@class = ""})</td>
                            <td>@q.Questions.Count()</td>
                            <td>@q.Locked</td>
                            <td>
                                <a href="#" data-id="@q.QuizId" class="delete-quiz">Delete</a>
                         
                      
                            </td>

                       
                        </tr>

                    }
                }


            </tbody>
        </table>
    }
    else
    {
        <div class="text-center">
            <br />
            <p><i>You have not created any quizzes yet.</i></p>
            @Html.ActionLink("Create", "Create", "Quizzes", routeValues: null, htmlAttributes: new {@class = "btn btn-primary"})

        </div>

    }
}

<script>
    
    

    function OnPostResultsError(jqXHR, textStatus, errorThrown) {
        ExpressQuiz.Utils.togglePreventLeavingPage(false);
        location.href = "/Home/Error/?message=" + errorThrown;
    }

    $(function() {
    
        $('.delete-quiz').click(function () {
            
            $.ajax({
                url: "/Quizzes/Delete/" + this.getAttribute("data-id"),
                type: 'POST',
                headers: ExpressQuiz.AjaxHelper.createRequestionVerificationTokenHeader(),
                success: function () {
                    location.reload(true);

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("could not delete quiz");

                }
            });

            return false;
        });
    });
</script>